package com.mxpio.erp.plan.service.impl;

import com.mxpio.erp.common.enums.CommonEnums;
import com.mxpio.erp.common.technology.entity.Item;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.mxpio.erp.common.plan.entity.ManufacturePlan;
import com.mxpio.erp.common.plan.service.ManufacturePlanService;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ManufacturePlanServiceImpl extends BaseServiceImpl<ManufacturePlan> implements ManufacturePlanService {

    @Transactional(readOnly = true)
    @Override
    public Page<ManufacturePlan> listPage(Pageable page, Criteria c){
        return JpaUtil.linq(ManufacturePlan.class)
                .collect("itemCode", Item.class, "itemCode")
                .where(c)
                .paging(page);
    }

    @Override
    @Transactional
    public Result<Object> changeToOutsourceFlag(List<ManufacturePlan> planList) {
        if (planList==null || planList.size()==0){
            return Result.error("提交数据为空");
        }
        List<String> planNos = planList.stream().map(ManufacturePlan::getPlanNo).collect(Collectors.toList());
        List<ManufacturePlan> dbPlanList = JpaUtil.linq(ManufacturePlan.class)
                .collect("itemCode", Item.class, "itemCode")
                .in("planNo", planNos).list();
        if (dbPlanList.size()>0){
            StringBuilder outSourceAbleCheckMsg = new StringBuilder();
            for (ManufacturePlan plan:dbPlanList){
                if (plan.getItem()!=null){
                    Item item = plan.getItem();
                    if (!StringUtils.equals(item.getOutsourceAble(),CommonEnums.YesNo.YES.getCode())){
                        outSourceAbleCheckMsg.append("【").append(item.getItemCode()).append(":").append(item.getItemName()).append("】");
                    }
                }
            }
            if (StringUtils.isNotBlank(outSourceAbleCheckMsg.toString())){
                return Result.error("计划中的物料基础信息，未开启委外属性。对应物料："+outSourceAbleCheckMsg);
            }
            for (ManufacturePlan plan:dbPlanList){
                if(!CommonEnums.YesNo.YES.getCode().equals(plan.getOutsourceFlag())){
                    plan.setOutsourceFlag(CommonEnums.YesNo.YES.getCode());
                    update(plan);
                }
            }
        }
        return Result.OK();
    }

    @Override
    @Transactional
    public Result<ManufacturePlan> split(String planNo, BigDecimal splitNum) {
        if (splitNum.compareTo(BigDecimal.ZERO)<=0){
            return Result.error("拆分数量为正整数！");
        }
        ManufacturePlan dbPlan = getById(ManufacturePlan.class, planNo);
        if (dbPlan==null){
            return Result.error("planNo无效，未能匹配到对应计划数据！");
        }
        BigDecimal quantity = dbPlan.getQuantity();
        if (splitNum.compareTo(quantity)>=0){
            return Result.error("拆分数量只能小于计划数量！");
        }
        quantity = quantity.subtract(splitNum);
        dbPlan.setQuantity(quantity);//修改原计划数量
        update(dbPlan);
        //复制计划
        ManufacturePlan copyPlan = new ManufacturePlan();
        BeanReflectionUtils.copyProperties(copyPlan,dbPlan);
        copyPlan.setPlanNo(null);
        copyPlan.setQuantity(splitNum);
        save(copyPlan);

        return Result.OK(copyPlan);
    }
}
